home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2MISC / CSDPMI1S.ZIP / SRC / CWSDPMI / DALLOC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-18  |  3.5 KB  |  162 lines

  1. /* Copyright (C) 1995 CW Sandmann (sandmann@clio.rice.edu) 102 Hurst Ct, Destrehan, LA 70047
  2. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  3. **
  4. ** This file is distributed under the terms listed in the document
  5. ** "copying.cws", available from CW Sandmann at the address above.
  6. ** A copy of "copying.cws" should accompany this file; if not, a copy
  7. ** should be available from where this file was obtained.  This file
  8. ** may not be distributed without a verbatim copy of "copying.cws".
  9. **
  10. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  11. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <dos.h>
  17. #include <io.h>
  18. #include <string.h>
  19.  
  20. #include "gotypes.h"
  21. #include "dalloc.h"
  22. #include "control.h"
  23.  
  24. #define DA_FREE    0
  25. #define DA_USED    1
  26.  
  27. static word8 map[MAX_DARRAY];
  28. static da_pn first_avail;
  29. static int dfile = -1;
  30. static da_pn disk_used,disk_max;
  31. static word16 paging_pid;
  32.  
  33. static void dset(da_pn i, int b)
  34. {
  35.   unsigned o;
  36.   word8 m;
  37.   o = (unsigned)(i>>3);
  38.   m = 1<<((unsigned)i&7);
  39.   if (b)
  40.     map[o] |= m;
  41.   else
  42.     map[o] &= ~m;
  43. }
  44.  
  45. static word8 dtest(da_pn i)
  46. {
  47.   unsigned o;
  48.   word8 m;
  49.   o = (unsigned)(i>>3);
  50.   m = 1<<((unsigned)i&7);
  51.   return map[o] & m;
  52. }
  53.  
  54. static char* dfilename;
  55.  
  56. void dalloc_file(char *swapname)
  57. {
  58.   dfilename = swapname;
  59. }
  60.  
  61. void dalloc_init(void)
  62. {
  63.   memset(map, 0, sizeof(map));
  64.   disk_used = disk_max = 0;
  65.   first_avail = MAX_DBLOCK + 1;        /* If no paging */
  66.   if(!dfilename || !dfilename[0]) {
  67.     SHOW_MEM_INFO("  No Paging.\n", 0);
  68.     return;
  69.   }
  70.   dfile = _creat(dfilename, 0);
  71.   paging_pid = get_pid();
  72.   if (dfile < 0)
  73.     fprintf(stderr, "Warning: cannot open swap file %s\n", dfilename);
  74.   else
  75.     first_avail = 0;
  76.   SHOW_MEM_INFO("  Swap space: %ld Kb\n", (dalloc_max_size() * 4L));
  77. }
  78.  
  79. void dalloc_uninit(void)
  80. {
  81.   if (dfile < 0)
  82.     return;
  83.   _close(dfile);
  84.   dfile = -1;
  85.   unlink(dfilename);
  86. }
  87.  
  88. da_pn dalloc(void)
  89. {
  90.   da_pn pn;
  91.   for (pn=first_avail; pn<=MAX_DBLOCK; pn++)
  92.     if (dtest(pn) == DA_FREE) {
  93.       dset(pn, DA_USED);
  94.       first_avail = pn+1;
  95.       if(first_avail > disk_max)
  96.         disk_max = first_avail;        /* pn+1 since zero based */
  97.       disk_used++;
  98.       return pn;
  99.     }
  100.   fprintf(stderr, "No swap space!\n");
  101.   cleanup(1);
  102.   return 0;
  103. }
  104.  
  105. void dfree(da_pn pn)
  106. {
  107.   dset(pn, DA_FREE);
  108.   if (pn < first_avail)
  109.     first_avail = pn;
  110.   disk_used--;
  111. }
  112.  
  113. da_pn dalloc_max_size(void)
  114. {
  115.   word32 fr;
  116.   word16 ax,bx,cx;
  117.   if (dfile < 0)
  118.     return 0;
  119.   _DL = dfilename[0] & 0x1f;
  120.   _AH = 0x36;
  121.   geninterrupt(0x21);
  122.   ax = _AX;        /* Sectors per cluster */
  123.   bx = _BX;        /* Number of available clusters */
  124.   cx = _CX;        /* bytes per sector */
  125.   if (ax == 0xffff)
  126.     return 0;
  127.   fr = (word32)(ax * cx) * (word32)bx;
  128.   fr /= 4096L;
  129.   fr += (unsigned long)disk_max;
  130.   if (fr > MAX_DBLOCK)
  131.     fr = MAX_DBLOCK;
  132.   return (da_pn)fr;
  133. }
  134.  
  135. da_pn dalloc_used(void)
  136. {
  137.   return (da_pn)disk_used;
  138. }
  139.  
  140. void dwrite(word8 *buf, da_pn block)
  141. {
  142.   int c;
  143.   word16 save_pid = get_pid();
  144.   set_pid(paging_pid);
  145.   lseek(dfile, (long)block*4096L, 0);
  146.   c = _write(dfile, buf, 4096);
  147.   set_pid(save_pid);
  148.   if (c < 4096) {
  149.     fprintf(stderr, "Swap disk full!\n");
  150.     cleanup(1);
  151.   }
  152. }
  153.  
  154. void dread(word8 *buf, da_pn block)
  155. {
  156.   word16 save_pid = get_pid();
  157.   set_pid(paging_pid);
  158.   lseek(dfile, (long)block*4096L, 0);
  159.   _read(dfile, buf, 4096);
  160.   set_pid(save_pid);
  161. }
  162.